home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / smailsrc.zip / UUPC.ZIP / DCPXFER.C < prev    next >
Text File  |  1990-04-21  |  8KB  |  425 lines

  1. /*
  2.     For    best results in    visual layout while viewing this file, set
  3.     tab stops to every 8 columns.
  4. */
  5.  
  6. /*
  7.     dcpxfer.c
  8.  
  9.     Revised edition of dcp
  10.  
  11.     Stuart Lynne May/87
  12.  
  13.     Copyright (c) Richard H. Lamb 1985,    1986, 1987
  14.     Changes Copyright (c) Stuart Lynne 1987
  15.  
  16.     Maintenance    Notes:
  17.  
  18.     01Nov87 - that strncpy should be a memcpy! - Jal
  19. */
  20.  
  21. /* "DCP" a uucp    clone. Copyright Richard H. Lamb 1985,1986,1987    */
  22.  
  23. /* file    send routines */
  24.  
  25. #include "dcp.h"
  26.  
  27. #include <ctype.h>
  28.  
  29. static unsigned    char rpacket[MAXPACK], spacket[MAXPACK];
  30.  
  31. static int S_size;  /* number of bytes in the spacket buffer */
  32.  
  33. static char fromfile[132], tofile[132];
  34.  
  35. static int bufill(), bufwrite(), getfile();
  36.  
  37.  
  38. /*************** SEND PROTOCOL ***************************/
  39.  
  40. /*
  41.     s d    a t a
  42.  
  43.     Send File Data
  44. */
  45.  
  46. char sdata()
  47. {
  48.  
  49.     for    ( ; ; )    {
  50.  
  51.     if ((*sendpkt)(spacket,    S_size,    0)) /* send data */
  52.         return 0;        /* trouble!    */
  53.  
  54.     if ((S_size = bufill(spacket)) == 0)    /* get data from file */
  55.         return 'Z';        /* if EOF set state    to that    */
  56.  
  57.     }
  58.  
  59. } /*sdata*/
  60.  
  61.  
  62. /*
  63.     b u    f i l l
  64.  
  65.     Get    a bufferful of data from the file that's being sent.
  66.     (Should perform input buffering here, perhaps 4K at    a time.)
  67. */
  68.  
  69. static int bufill(buffer)
  70. char *buffer;
  71. {
  72.  
  73.     return read(fp, buffer, pktsize);    /* Handle partial buffer */
  74.  
  75. } /*bufill*/
  76.  
  77.  
  78. /*
  79.     b u    f w r i    t e
  80.  
  81.     Write a bufferful of data to the file that's being received.
  82.     (Should perform output buffering here, perhaps 4K at a time.)
  83. */
  84.  
  85. static int bufwrite(buffer, len)
  86. char *buffer;
  87. int len;
  88. {
  89.  
  90.     return write(fp, buffer, len);  /* Write incoming data to file */
  91.  
  92. } /*bufwrite*/
  93.  
  94.  
  95. /*
  96.     s b    r e a k
  97.  
  98.     Send Break (EOT)
  99. */
  100.  
  101. char sbreak()
  102. {
  103.     int    len;
  104.  
  105.     strcpy(spacket, "H");
  106.     if ((*sendpkt)(spacket, 0, 1))
  107.     return 0;
  108.  
  109.     if ((*getpkt)(spacket, &len))
  110.     return 0;
  111.  
  112.     printmsg(2,    "Switching modes.");
  113.  
  114.     return (spacket[1] == 'N') ? 'G' : 'Y';
  115.  
  116. } /*sbreak*/
  117.  
  118. /*
  119.     s e    o f
  120.  
  121.     Send End-Of-File
  122. */
  123.  
  124. char seof()
  125. {
  126.     char hostfile[132];
  127.     int    len;
  128.  
  129.     if ((*sendpkt)(spacket, 0, 0))
  130.     return 0;
  131.  
  132.     if ((*getpkt)(spacket, &len))
  133.     return 0;   /* receive CY or CN    */
  134.     if (!equaln(spacket, "CY", 2))
  135.     return 0;   /* can't send file */
  136.  
  137.     close(fp);
  138.     fp = -1;
  139.     importpath(hostfile, fromfile);
  140.     unlink(hostfile);
  141.  
  142.     printmsg(0,    "Transfer of %s    (%s) completed.", fromfile, hostfile);
  143.  
  144.     /*   Have to write the SYSLOG file someday!
  145.     fprintf(syslog, "%s!%s (%d/%d-%d:%d:%d) -> %ld / %ld secs",
  146.     host, id, month, day, hour, minute, seconds, size, secs);
  147.     */
  148.  
  149.     return 'F';     /* go get the next file to send */
  150.  
  151. } /*seof*/
  152.  
  153.  
  154. /*
  155.     s f    i l e
  156.  
  157.     Send File Header
  158. */
  159.  
  160. char sfile()
  161. {
  162.     char hostfile[132];
  163.     int    len;
  164.  
  165.     if (fp == -1) { /* if not already open */
  166.  
  167.     printmsg(3, "looking in    work file...");
  168.     if (getfile()) {    /* get next    request    from current work file */
  169.         fclose(fwork);
  170.         fwork = nil(FILE);
  171.         unlink(workfile);    /* delete completed call file */
  172.         return 'B';        /* end sending session */
  173.     }
  174.  
  175.     importpath(hostfile, fromfile);
  176.     printmsg(3, "Opening %s    (%s) for sending.", fromfile, hostfile);
  177.     if ((fp    = open(hostfile, 0)) ==    -1) {    /* open    file to    sent */
  178.         printmsg(0,    "Cannot    open file %s (%s).", fromfile, hostfile);
  179.         return 'A';
  180.     }
  181.  
  182.     } else
  183.     return 'A'; /* Something's already open.  We're    in trouble! */
  184.  
  185.     printmsg(0,    "Sending %s (%s) as %s.", fromfile, hostfile, tofile);
  186.  
  187.     strcpy(spacket, tofile);
  188.     if ((*sendpkt)(spacket, 0, 1))
  189.     return 0;    /* send    'S fromfile tofile user    - tofile 0666' */
  190.  
  191.     if ((*getpkt)(spacket, &len))
  192.     return 0;
  193.  
  194.     if (spacket[1] != 'Y')
  195.     return 'A';    /* If other side says no, then quit */
  196.  
  197.     S_size = bufill(spacket);
  198.  
  199.     return 'D';
  200.  
  201. } /*sfile*/
  202.  
  203.  
  204. /*
  205.     s i    n i t
  206.  
  207.     Send Initiate: send    this host's parameters and get other side's back.
  208. */
  209.  
  210. char sinit()
  211. {
  212.  
  213.     return (*openpk)() ? 'A' : 'B';
  214.  
  215. } /*sinit*/
  216.  
  217.  
  218. /*
  219.     g e    t f i l    e
  220.  
  221.     Reads the next line    from the presently open    call file
  222.     (*workfile)    and determines from this the next file to be sent
  223.     (*fromfile).  If there are no more,    TRUE is    returned.
  224.  
  225.     ** A fix for "R from to 0666" should be done here to recieve files
  226.     in addition    to sending them.  The appropriate "state letter"
  227.     i.e. "R" should be returned    to the send "master" or    "slave"
  228.     state switching table in "dcp.c".
  229.     I did not implement    this since the majority    of uucp    transactions
  230.     appear to be "S from to 0666" type.     R.H.Lamb 1/87
  231. */
  232.  
  233. static int getfile()
  234. {
  235.     int    i;
  236.     char line[132];
  237.     register char *cp;
  238.  
  239.     if (fgets(line, BUFSIZ, fwork) == nil(char))
  240.     return TRUE;
  241.  
  242.     sscanf(line    + 2, "%s ", fromfile);
  243.     for    (i = 0,    cp = line; *cp!='\0'; i++, cp++) {
  244.     if (equaln(cp, "0666", 4))
  245.         break;
  246.     }
  247.     cp += 4;
  248.     *cp    = '\0';
  249.     strcpy(tofile, line);
  250.     printmsg(3,    "getfile: fromfile=%s, tofile=%s.", fromfile, tofile);
  251.  
  252.     return FALSE;
  253.  
  254. } /*getfile*/
  255.  
  256.  
  257. /*********************** MISC SUB SUB PROTOCOL *************************/
  258.  
  259. /*
  260.     s c    h k d i    r
  261.  
  262.     scan spooling directory for    C.* files for the other    system
  263. */
  264.  
  265. char schkdir()
  266. {
  267.     char c;
  268.  
  269.     if ((c = scandir(rmtname)) == 'Q')
  270.     return 'Y';
  271.  
  272.     if (c == 'S') {
  273.     strcpy(rpacket,    "HN");
  274.     if ((*sendpkt)(rpacket,    0, 1))
  275.         return 0;
  276.     }
  277.  
  278.     return 'B';
  279.  
  280. } /*schkdir*/
  281.  
  282.  
  283. /*
  284.     e n    d p
  285.  
  286.     end    the protocol
  287. */
  288.  
  289. char endp()
  290. {
  291.  
  292.     strcpy(rpacket, "HY");
  293.     (*sendpkt)(rpacket,    0, 2);    /* don't wait for ACK */
  294.     (*closepk)();
  295.  
  296.     return 'P';
  297.  
  298. } /*endp*/
  299.  
  300.  
  301. /*********************** RECIEVE PROTOCOL **********************/
  302.  
  303. /*
  304.     r d    a t a
  305.  
  306.     Receive Data
  307. */
  308.  
  309. char rdata()
  310. {
  311.     int    len;
  312.  
  313.     if ((*getpkt)(rpacket, &len))
  314.     return 0;
  315.  
  316.     if (len == 0) {
  317.     close(fp);
  318.     strcpy(rpacket,    "CY");
  319.     if ((*sendpkt)(rpacket,    0, 1))
  320.         return 0;
  321.     printmsg(0, "Transfer completed.");
  322.     return 'F';
  323.     }
  324.  
  325.     /* write incoming data to the file */
  326.     if (bufwrite(rpacket, len) == -1) {
  327.     printmsg(0, "Error writing data    to file.");
  328.     return 'A';
  329.     }
  330.  
  331.     return 'D';    /* Remain in data state    */
  332.  
  333. } /*rdata*/
  334.  
  335.  
  336. /*
  337.     r f    i l e
  338.  
  339.     Receive File Header
  340. */
  341.  
  342. char rfile()
  343. {
  344.     char buf[256], tmpfilename[256];
  345.     char *flds[10], *cp;
  346.  
  347.     printmsg(3,    "rfile:    entered");
  348.  
  349.     for    (cp = buf; ; ) {
  350.     int len;
  351.     if ((*getpkt)(rpacket, &len))
  352.         return 0;
  353.     memcpy(cp, rpacket, len);
  354.     cp += len;
  355.     if (cp[-1] == '\0')
  356.         break;
  357.     }
  358.  
  359.     if ((buf[0]    & 0x7f)    == 'H')
  360.     return 'C';    /* the other side (master) is done */
  361.  
  362.     printmsg(3,    "rfile:    command    \"%s\"", buf);
  363.  
  364.     getargs(buf, flds);
  365.  
  366.     cp = flds[2];
  367.     printmsg(3,    "rfile:    destination \"%s\"", cp);
  368.  
  369.     /* trim leading ~/ off if it's there */
  370.     if (equaln(cp, "~/", 2))
  371.     strcpy(tmpfilename, cp + 1);
  372.     else
  373.     strcpy(tmpfilename, cp);
  374.  
  375.     /* check if    the name is a directory    name (end with a '/') */
  376.     if (cp[strlen(cp) -    1] == '/') {
  377.     printmsg(3, "rfile: destination    is directory \"%s\"", flds[1]);
  378.     if ((cp    = strrchr(flds[1], '/')) == nil(char))
  379.         cp = flds[1];
  380.     else
  381.         cp++;
  382.     printmsg(3, "rfile: dironly add    \"%s\"", cp);
  383.     strcat(tmpfilename, cp);
  384.     }
  385.     printmsg(3,    "rfile:    receive    file \"%s\"", tmpfilename);
  386.  
  387.     /* let host    munge filename as appropriate */
  388.     importpath(tofile, tmpfilename);
  389.     printmsg(3,    "rfile:    host file \"%s\"", tofile);
  390.  
  391.     if (equaln(flds[2],    "~/", 2)) {
  392.     mkfilename(tmpfilename,    pubdir,    tofile);
  393.     strcpy(tofile, tmpfilename);
  394.     printmsg(3, "rfile: ~/ expansion \"%s\"", tofile);
  395.     }
  396.  
  397.     if ((fp = CREAT(tofile, 0775, BINARY)) == -1) { /* open a new file */
  398.     printmsg(0, "cannot create %s",    tofile);
  399.     return 'A';    /* Give    up if we can't */
  400.     }
  401.  
  402.     printmsg(1,    "Receiving \"%s\" as \"%s\"", flds[1], tofile);
  403.  
  404.     strcpy(rpacket, "SY");
  405.     if ((*sendpkt)(rpacket, 0, 1))
  406.     return 0;
  407.  
  408.     return 'D';    /* Switch to data state    */
  409.  
  410. } /*rfile*/
  411.  
  412.  
  413. /*
  414.     r i    n i t
  415.  
  416.     Receive Initialization
  417. */
  418.  
  419. char rinit()
  420. {
  421.  
  422.     return ((*openpk)()) ? 0 : 'F';
  423.  
  424. } /*rinit*/
  425.